# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName : p21.py
# @Time     : 2024/5/12 18:17
# @Author   : Robot-Zsj
"""
description:

电脑病毒感染

https://fcqian.blog.csdn.net/article/details/134386911

i:
4
3
2 1 1
2 3 1
3 4 1
2

o:
2
"""

import sys

n = int(input())

graph = {}

m = int(input())
for i in range(m):
    u, v, w = map(int, input().split())
    graph.setdefault(u, [])
    graph[u].append([v, w])

dist = [sys.maxsize] * (n + 1)

src = int(input())

dist[src] = 0

needCheck = [src]

visited = [False] * (n + 1)
visited[src] = True

while len(needCheck) > 0:
    cur = needCheck.pop()
    visited[cur] = False

    if graph.get(cur) is not None:
        for v, w in graph[cur]:
            newDist = dist[cur] + w
            if dist[v] > newDist:
                dist[v] = newDist
                if not visited[v]:
                    visited[v] = True
                    needCheck.append(v)
                    needCheck.sort(key=lambda x: -dist[x])
ans = max(dist[1:])
print(-1 if ans == sys.maxsize else ans)
